package csse3004.GD.SysAdmin;

/**
 * @author Mamdouh Alharbi s4194298
 */
import org.springframework.validation.Validator;
import org.springframework.validation.Errors;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UserSearchValidator implements Validator {

    protected final Log logger = LogFactory.getLog(getClass());

    public boolean supports(Class clazz) {
        return UserSearch.class.equals(clazz);
    }

    public void validate(Object obj, Errors errors) {

        UserSearch searchy = (UserSearch) obj;
        String searchWord = searchy.getSearchword();
        String searchType = searchy.getSearchtype();
        logger.info(searchType);
        if (searchType.equals("organisation")) {
            logger.info("Checking Empty String");

            if (searchWord.isEmpty()) {
                errors.rejectValue("searchword", "error.not-specified", null, "Search Word is Required.");
            }
        }
        logger.info("passed validation");

    }
}
